<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Auth;

class Product extends Model
{

    protected $casts = [
        'pic_group' => 'array',
        'logistics_mode' => 'array'
    ];

    /**
     * 应被转换为日期的属性。
     *
     * @var array
     */
    protected $dates = [
        'created_at',
        'updated_at',
        'deleted_at'
    ];

    // 获得拥有此货品的店铺。
    public function shop()
    {
        return $this->belongsTo('App\Models\Shop', 'shop_id');
    }

    /**
     * 限制查询上架的商品。
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeSelling($query)
    {
        return $query->where('status', 1);
    }

    /**
     * 限制查询上架的商品。
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopePublished($query)
    {
        return $query->where('status', 1);
    }


    /**
     * 限制自营。
     *
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeSelfSupport($query)
    {
        return $query->where('shop_id', 1);
    }


    /**
     * 限制不是自营
     * 
     * @return \Illuminate\Database\Eloquent\Builder
     */
    public function scopeNormal($query)
    {
        return $query->where('shop_id', '<>', 1);
    }
}
